<template>
  <div class="app-container">
    <el-collapse-transition>
      <el-form v-if="openCondition" :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="规则名称" prop="ruleName">
          <el-input
            v-model="queryParams.ruleName"
            placeholder="请输入规则名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="规则编码" prop="ruleCode">
          <el-input
            v-model="queryParams.ruleCode"
            placeholder="请输入规则编码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="编码类型" prop="formatType">
          <el-select v-model="queryParams.formatType" placeholder="请选择编码类型" clearable size="small">
            <el-option
              v-for="dict in formatTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </el-collapse-transition>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:codeRule:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:codeRule:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:codeRule:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5" style="float: right">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :type="!openCondition?'success':'info'"
                   :icon="!openCondition?'el-icon-caret-bottom':'el-icon-caret-top'" size="mini"
                   @click="openCondition=!openCondition">{{!openCondition?'展开条件':'收起条件'}}
        </el-button>
      </el-col>
    </el-row>

    <el-table :data="codeRuleList" height="580" @sort-change="handleSortChange"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" width="50">
        <template scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="规则名称" align="center" prop="ruleName"/>
      <el-table-column label="规则编码" align="center" prop="ruleCode"/>
      <el-table-column label="编码前缀" align="center" prop="rulePrefix"/>
      <el-table-column label="编码类型" align="center" sortable="custom" prop="formatType">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.formatType.value }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="日期格式" align="center" prop="formatStr"/>
      <el-table-column label="当前日期" align="center" prop="curFormatVal"/>
      <el-table-column label="序列占位长度" align="center" prop="serialLen"/>
      <el-table-column label="当前序列值" align="center" sortable="custom" prop="serial"/>
      <el-table-column label="操作" align="center" fixed="right" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleView(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:codeRule:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:codeRule:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统编码规则配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="750px">
      <!-- 修改 -->
      <el-form v-if="this.edit" ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="规则名称" prop="ruleName">
          <el-input v-model="form.ruleName" placeholder="请输入规则名称"/>
        </el-form-item>
        <el-form-item label="规则编码" prop="ruleCode">
          <el-input v-model="form.ruleCode" placeholder="请输入规则编码"/>
        </el-form-item>
        <el-form-item label="编码前缀" prop="rulePrefix">
          <el-input v-model="form.rulePrefix" placeholder="请输入编码前缀"/>
        </el-form-item>
        <el-form-item label="编码类型" prop="formatType">
          <el-select v-model="form.formatType" placeholder="请选择编码类型">
            <el-option
              v-for="dict in formatTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="格式化格式" prop="formatStr">
          <el-input v-model="form.formatStr" placeholder="请输入格式化格式"/>
        </el-form-item>
        <el-form-item label="当前日期" prop="curFormatVal">
          <el-input v-model="form.curFormatVal" placeholder="请输入当前日期"/>
        </el-form-item>
        <el-form-item label="序列占位长度" prop="serialLen">
          <el-input v-model="form.serialLen" placeholder="请输入序列占位长度"/>
        </el-form-item>
        <el-form-item label="当前序列值" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入当前序列值"/>
        </el-form-item>
      </el-form>
      <!-- 添加 -->
      <el-form v-if="!this.edit" ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="规则名称" prop="ruleName">
          <el-input v-model="form.ruleName" placeholder="请输入规则名称"/>
        </el-form-item>
        <el-form-item label="规则编码" prop="ruleCode">
          <el-input v-model="form.ruleCode" placeholder="请输入规则编码"/>
        </el-form-item>
        <el-form-item label="编码前缀" prop="rulePrefix">
          <el-input v-model="form.rulePrefix" placeholder="请输入编码前缀"/>
        </el-form-item>
        <el-form-item label="编码类型" prop="formatType">
          <el-select v-model="form.formatType" placeholder="请选择编码类型">
            <el-option
              v-for="dict in formatTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="格式化格式" prop="formatStr">
          <el-input v-model="form.formatStr" placeholder="请输入格式化格式"/>
        </el-form-item>
        <el-form-item label="当前日期" prop="curFormatVal">
          <el-input v-model="form.curFormatVal" placeholder="请输入当前日期"/>
        </el-form-item>
        <el-form-item label="序列占位长度" prop="serialLen">
          <el-input v-model="form.serialLen" placeholder="请输入序列占位长度"/>
        </el-form-item>
        <el-form-item label="当前序列值" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入当前序列值"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 系统编码规则配置详情 -->
    <el-dialog title="系统编码规则配置详情" :visible.sync="viewOpen" width="750px">
      <el-form :inline="true" class="detail-inline" label-width="140px">
        <el-form-item label="规则名称：">
          <input class="bird-view-info-input" disabled :title="viewData.ruleName" :value="viewData.ruleName"/>
        </el-form-item>
        <el-form-item label="规则编码：">
          <input class="bird-view-info-input" disabled :title="viewData.ruleCode" :value="viewData.ruleCode"/>
        </el-form-item>
        <el-form-item label="编码前缀：">
          <input class="bird-view-info-input" disabled :title="viewData.rulePrefix" :value="viewData.rulePrefix"/>
        </el-form-item>
        <el-form-item label="编码类型：">
          <input class="bird-view-info-input" disabled :title="viewData.formatType" :value="viewData.formatType"/>
        </el-form-item>
        <el-form-item label="格式化格式：">
          <input class="bird-view-info-input" disabled :title="viewData.formatStr" :value="viewData.formatStr"/>
        </el-form-item>
        <el-form-item label="当前日期：">
          <input class="bird-view-info-input" disabled :title="viewData.curFormatVal" :value="viewData.curFormatVal"/>
        </el-form-item>
        <el-form-item label="序列占位长度：">
          <input class="bird-view-info-input" disabled :title="viewData.serialLen" :value="viewData.serialLen"/>
        </el-form-item>
        <el-form-item label="当前序列值：">
          <input class="bird-view-info-input" disabled :title="viewData.serial" :value="viewData.serial"/>
        </el-form-item>
        <el-form-item label="创建人：">
          <input class="bird-view-info-input" disabled :title="viewData.creater" :value="viewData.creater"/>
        </el-form-item>
        <el-form-item label="删除标志：">
          <input class="bird-view-info-input" disabled :title="viewData.deleted" :value="viewData.deleted"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false" size="mini">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {addCodeRule, delCodeRule, getCodeRule, listCodeRule, updateCodeRule} from "@/api/system/codeRule";

  export default {
    data() {
      return {
        // 条件展开
        openCondition: true,
        // 是否修改
        edit: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 系统编码规则配置表格数据
        codeRuleList: [],
        // 弹出层标题
        title: "",
        // 是否显示详情弹出层
        viewOpen: false,
        // 是否显示弹出层
        open: false,
        // 编码类型字典
        formatTypeOptions: [],
        // 删除标志字典
        deletedOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderByColumn: 'id',
          isAsc: 'desc'
        },
        // 详情参数
        viewData: {},
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          ruleName: [
            {required: true, message: "规则名称不能为空", trigger: "blur"}
          ],
          ruleCode: [
            {required: true, message: "规则编码不能为空", trigger: "blur"}
          ],
          formatType: [
            {required: true, message: "编码类型不能为空", trigger: "blur"}
          ],
          serialLen: [
            {required: true, message: "序列占位长度不能为空", trigger: "blur"}
          ],
          serial: [
            {required: true, message: "当前序列值不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("CodeRuleTypeEnum").then(response => {
        this.formatTypeOptions = response.data;
      });
      this.getDicts("FlagEnum").then(response => {
        this.deletedOptions = response.data;
      });
    },
    methods: {
      /** 查询系统编码规则配置列表 */
      getList() {
        listCodeRule(this.queryParams).then(response => {
          this.codeRuleList = response.data.list;
          this.total = response.data.total;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.resetPropField(this.form);
        this.clearForm('form');
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetPropField(this.queryParams, {pageNum: 1, pageSize: 10, orderByColumn: 'id', isAsc: 'desc'});
        this.handleQuery();
      },
      // 排序改变事件
      handleSortChange(sorter) {
        this.handleSort(this.queryParams.sorter)
        this.getList();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 详细按钮操作 */
      handleView(row) {
        const id = row.id
        getCodeRule(id).then(response => {
          const {data} = response;
          this.viewData = {
            ...data,
            formatType: data.formatType && data.formatType.value,
            deleted: data.deleted && data.deleted.value,
          }
          this.viewOpen = true;
        });
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.edit = false;
        this.open = true;
        this.title = "添加系统编码规则配置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.edit = true;
        this.reset();
        const id = row.id || this.ids
        getCodeRule(id).then(response => {
          const {data} = response;
          this.form = {
            ...data,
            formatType: data.formatType && data.formatType.key,
            deleted: data.deleted && data.deleted.key,
          }
          this.open = true;
          this.title = "修改系统编码规则配置";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateCodeRule(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCodeRule(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除系统编码规则配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delCodeRule(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
      }
    }
  };
</script>
